package com.retlife.tvshow.ui

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.retlife.tvshow.R
import com.retlife.tvshow.global.EventBusMsg
import com.retlife.tvshow.global.EventBusOrder
import com.retlife.tvshow.global.RunningContext
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainActivity : BaseActivity() {
    private lateinit var recyclerView: RecyclerView
    private var adapter: BikeDataAdapter = BikeDataAdapter()
    private var quitDialog: QuitDialog? = null

    //rotate speed Torque
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        recyclerView = findViewById(R.id.layout_bike_info)
        initBikeData()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
        val widthPixels = windowManager.defaultDisplay.width
        val heightPixels = windowManager.defaultDisplay.height
        val xdpi = resources.displayMetrics.density
        val ydpi = resources.displayMetrics.ydpi
        Log.e(javaClass.simpleName, "xdpi: $xdpi, ydpi: $ydpi")
        Log.e(javaClass.simpleName, "widthPixels: $widthPixels, heightPixels: $heightPixels")
    }

    private fun initBikeData() {
        val maxConnect = RunningContext.maxConnect
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (event?.action == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                startActivity(Intent(this, GameActivity::class.java))
                return true
            } else if (keyCode == KeyEvent.KEYCODE_BACK) {
                showQuit()
                return true
            }
            /*else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                if (quitDialog?.isVisible() == true) {
                    finish()
                }
                return true
            }else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                if (quitDialog?.isVisible() == true) {
                    quitDialog?.dismiss()
                }
                return true
            }*/
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun showQuit() {
        if (quitDialog == null) {
            quitDialog = QuitDialog(this)
            quitDialog?.show()
        } else {
            if (quitDialog?.isVisible() == false) {
                quitDialog?.show()
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(event: EventBusMsg) {
        when (event.tag) {

            EventBusOrder.TIMER -> {
                runOnUiThread {
                    adapter.notifyDataSetChanged()
                }
            }
            EventBusOrder.ACTION_QUIT -> {
                if (quitDialog?.isVisible() == true) {
                    quitDialog?.dismiss()
                    finish()
                }
            }

            EventBusOrder.ACTION_RESTART -> {
                if (quitDialog?.isVisible() == true) {
                    quitDialog?.dismiss()
                }
            }
        }
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }

    fun game(view: View) {
        startActivity(Intent(this, GameActivity::class.java))

    }
}